package top.yxf.gulimall.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.yxf.common.utils.R;
import top.yxf.gulimall.product.entity.BrandEntity;
import top.yxf.gulimall.product.entity.CategoryBrandRelationEntity;
import top.yxf.gulimall.product.service.CategoryBrandRelationService;
import top.yxf.gulimall.product.vo.BrandVo;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName CategoryBrandRelationController
 * @Description TODO
 * @Author zw
 * @Date 2021/10/27 下午10:15
 * @Version 1.0
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {

    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * product/categorybrandrelation/brands/list
     * Controller: 处理请求 接收校验数据
     * Service： 接收Controller传的数据，进行业务处理
     * 3、Controller接收Service处理完的数据，封装页面响应的vo
     */
    @GetMapping(value = "/brands/list")
    public R relationBrandsList(@RequestParam(value = "catId", required = true) Long catId) {

        List<BrandEntity> list = categoryBrandRelationService.getBrandsByCatId(catId);

        if (null != list && list.size() > 0) {
            List<BrandVo> brandVos = list.stream().map(brandEntity -> {
                BrandVo brandVo = new BrandVo();
                brandVo.setBrandId(brandEntity.getBrandId());
                brandVo.setBrandName(brandEntity.getName());
                return brandVo;
            }).collect(Collectors.toList());
            return R.ok().put("data", brandVos);
        }

        return R.ok().put("data", new ArrayList<>());

    }


    @RequestMapping(value = "/catelog/list", method = RequestMethod.GET)
    public R list(@RequestParam("brandId") Long brandId) {

        List<CategoryBrandRelationEntity> data = categoryBrandRelationService.list(brandId);
        return R.ok().put("data", data);
    }

    @RequestMapping(value = "/save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelationEntity) {

        categoryBrandRelationService.saveDetail(categoryBrandRelationEntity);
        return R.ok();
    }

//    @RequestMapping(value = "/update")
//    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelationEntity) {
//
//        categoryBrandRelationService.updateDetail(categoryBrandRelationEntity);
//        return R.ok();
//    }


}
